package com.itheima.controller;

/**
 * @author 马祥和
 * @version 1.0
 * @function
 * @time 2025/7/29 16:45
 */

import com.itheima.entity.Clazz;
import com.itheima.entity.ClazzQueryParam;
import com.itheima.entity.PageBean;
import com.itheima.entity.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class MyClazzController {
    @Autowired
    private ClazzService clazzService;

    @GetMapping
    public Result list(ClazzQueryParam cqp){
        log.info("分页查询: {}, {},{}, {}, {}", cqp.getName(), cqp.getBegin(), cqp.getEnd(), cqp.getPage(), cqp.getPageSize());
        PageBean pageBean = clazzService.list2(cqp);
        if(pageBean.getRows().isEmpty() || pageBean.getTotal()==0){
            return Result.error("未找到数据");
        }
        return Result.success(pageBean);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        log.info("删除班级id:{}",id);

        clazzService.deleteById(id);

        return Result.success();
    }

    @PostMapping
    public Result insert(@RequestBody Clazz clazz){
        log.info("增加班级:{}",clazz);

        clazzService.insert(clazz);

        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        log.info("查询班级id:{}",id);
        Clazz clazz = clazzService.findById(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result update(@RequestBody Clazz clazz){

        clazzService.update(clazz);
        return Result.success();
    }

    @GetMapping("list")
    public Result allList(){

        List<Clazz> clazzList = clazzService.allList();
        return Result.success(clazzList);
    }

}
